package restless.utilities.testing {
	import flash.display.*;
	import flash.utils.*;
	import flash.text.*;
 
	public class LookUpSpeed extends Sprite
	{
		private var __logger:TextField = new TextField();
		private function row(...cols): void
		{
			__logger.appendText(cols.join(", ") + "\n");
		}
 
		public function LookUpSpeed()
		{
			__logger.autoSize = TextFieldAutoSize.LEFT;
			addChild(__logger);
 
			var beforeTime:int;
			var afterTime:int;
			var noCastTime:int;
			var castTime:int;
			var item:MyItem;
			var i:uint;
			var len:uint = 1000000;
			var itemsArray:Array = new Array(len);
			var itemsVector:Vector.<MyItem> = new Vector.<MyItem>(len);
			var itemsDictionary:Dictionary = new Dictionary();
			var itemsObject:Object = new Object();
			var itemsDynClass:Dynamic = new Dynamic();
			var string:String = "MyItem";
			
			for (i = 0; i < len; ++i)
			{
				//TODO: test speed of dictionary string lookup against look up of a string vector
				itemsArray[i] = 
				itemsVector[i] = 
				itemsDictionary[i] = 
				itemsObject[i] = 
				itemsDynClass[i] = new MyItem;
			}
 
			row("Type", "No Cast Time", "Cast Time");
 
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsArray[i];
			}
			afterTime = getTimer();
			noCastTime = afterTime-beforeTime;
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsArray[i] as MyItem;
			}
			afterTime = getTimer();
			castTime = afterTime-beforeTime;
			row("Array", noCastTime, castTime);
 
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsVector[i];
			}
			afterTime = getTimer();
			noCastTime = afterTime-beforeTime;
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsVector[i] as MyItem;
			}
			afterTime = getTimer();
			castTime = afterTime-beforeTime;
			row("Vector", noCastTime, castTime);
 
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsDictionary[i];
			}
			afterTime = getTimer();
			noCastTime = afterTime-beforeTime;
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsDictionary[i] as MyItem;
			}
			afterTime = getTimer();
			castTime = afterTime-beforeTime;
			row("Dictionary", noCastTime, castTime);
 
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsObject[i];
			}
			afterTime = getTimer();
			noCastTime = afterTime-beforeTime;
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsObject[i] as MyItem;
			}
			afterTime = getTimer();
			castTime = afterTime-beforeTime;
			row("Object", noCastTime, castTime);
 
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsDynClass[i];
			}
			afterTime = getTimer();
			noCastTime = afterTime-beforeTime;
			beforeTime = getTimer();
			for (i = 0; i < len; ++i)
			{
				item = itemsDynClass[i] as MyItem;
			}
			afterTime = getTimer();
			castTime = afterTime-beforeTime;
			row("Dynamic Class", noCastTime, castTime);
		}
	}
}
class MyItem{}
dynamic class Dynamic{}